package com.aisi.entity;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;

/**
 * @Author: shenjianZ
 * @Date: 2024/2/14 13:07
 * @Description: 这段代码定义了一个名为RestBean的Java记录(record),
 *               它是一个泛型类,这个类有四个静态方法，
 *               分别用于创建成功、失败、未授权和禁止访问的RestBean对象。
 */


public record RestBean<T>(int code, T data, String message) {

    public static <T> RestBean<T> success(T data) {
        return new RestBean<>(200, data, "请求成功");
    }

    public static <T> RestBean<T> success() {
        return success(null);
    }

    public static <T> RestBean<T> failure(int code, String message) {
        return new RestBean<>(code, null, message);
    }

    public static <T> RestBean<T> unauthorized(String message){
        return  failure(401, message);
    }

    public static <T> RestBean<T> forbidden(String message){
        return  failure(403, message);
    }

    public String asJsonString() {
        return JSON.toJSONString(this, JSONWriter.Feature.WriteNulls);

    }
}
